"""
    @Author  ：思念 
    @File    ：2.协程爬虫.py
    @Date    ：2024/12/23 20:12 
"""
import requests
import asyncio
from functools import partial
from bs4 import BeautifulSoup

url = 'https://movie.douban.com/top250?start={}&filter='
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
}


async def get_movie_info(page_num):
    """
        1.将协程放入线程池中执行
            run_in_executor(None)：使用默认的线程池
            partial（偏函数）：函数的参数进行固定并返回一个新函数
    """
    response = await loop.run_in_executor(None, partial(requests.get, url.format(page_num * 25), headers=headers))
    soup = BeautifulSoup(response.text, 'lxml')
    div_list = soup.find_all('div', class_='hd')
    for title in div_list:
        print(title.get_text())

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [loop.create_task(get_movie_info(page)) for page in range(10)]
    loop.run_until_complete(asyncio.wait(tasks))
